﻿using Acme.BookStore.RoomNos;
using Acme.BookStore.RoomTypes;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Dishes
{
    
    /// <summary>
    ///实现菜品接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class DishesServices : ApplicationService, IDishesServices
    {
        /// <summary>
        /// 注入
        /// </summary>
        private readonly IRepository<DishesInfo> repository;
        private readonly IRepository<DishesGrouping> repository1;
        private readonly IWebHostEnvironment env;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="repository1"></param>
        /// <param name="env"></param>
        public DishesServices(IRepository<DishesInfo> repository,IRepository<DishesGrouping> repository1, IWebHostEnvironment env)
        {
            
            this.repository = repository;
            this.repository1 = repository1;
            this.env = env;
        }



        /// <summary>
        /// 添加菜品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("PostDishes")]
        public async Task<ApiResult> PostDishes(DishesDTO dto)
        {
            try
            {
                try
                {
                    var list = ObjectMapper.Map<DishesDTO,DishesInfo>(dto);
                    await repository.InsertAsync(list);
                    return ApiResult.Ok(data: list);
                }
                catch (Exception ex)
                {
                    ex.HelpLink = "操作失败";
                    return ApiResult.Fail(msg: ex.Message);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        ///菜品显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("GetDishesGrouping")]
        public async Task<ApiResult> GetDishesGrouping(string? DishesName)
        {
            var Dish = await repository.GetListAsync();
            var DishGrouping = await repository1.GetListAsync();

            var list = from a in Dish
                       join b in DishGrouping
                       on a.DishesGroupingId equals b.Id
                       where a.IsDeleted == false
                       select new DishesShowDTO
                       {
                           Id = a.Id,
                           DishesName= a.DishesName,
                           DishesImg=a.DishesImg,
                           DishesDesc=a.DishesDesc,
                           DishesGroupingId=b.Id,
                           DishesGroupingName=b.DishesGroupingName,
                           DishesTaste=a.DishesTaste,
                           SaleDate=a.SaleDate,
                           SalesPrice=a.SalesPrice,
                           SumSales=a.SumSales,
                           Inventory=a.Inventory
                       };
            //菜品名称模糊查询
            if(!string.IsNullOrEmpty(DishesName))
            {
                list = list.Where(x => x.DishesName.Contains(DishesName));
            }
            return ApiResult.Ok(list);
            
        }


        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        [HttpPost("PostImag")]
        public string PostImag(IFormFile file)
        {
            var root = env.ContentRootPath;
            var newlist = root + "/wwwroot/Files/" + file.FileName;
            using (FileStream fs = System.IO.File.Create(newlist))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return "https://localhost:44392" + "/Files/" + file.FileName;
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">数组</param>
        /// <returns>返回任务</returns>
        [HttpDelete("DeleteDishes")]
        public async Task<ApiResult> DeleteDishes(List<Guid> ids)
        {
            var list = await repository.GetListAsync(m => ids.Contains(m.Id));

            await repository.DeleteManyAsync(list);
            return ApiResult.Ok(data: list);
        }

        /// <summary>
        /// 删除 
        /// </summary>
        /// <param name="Id">菜品删除</param>
        /// <returns></returns>
        [HttpDelete("DeleteDishe")]
        public async Task<ApiResult> DeleteDisheAsync(Guid Id)
        {
            try
            {
                await repository.DeleteAsync(x=>x.Id == Id);
                return ApiResult.Ok();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 菜品修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut("PutDishes")]
        public async Task<ApiResult> PutDishes(UpdateDishesDTO dto)
        {
            var list = await repository.FirstOrDefaultAsync(x => x.Id == dto.Id);
            if(list==null)
            {
                return ApiResult.Fail(msg: "菜品不能为空");
            }
            ObjectMapper.Map(dto, list);
            await repository.UpdateAsync(list);
            return ApiResult.Ok(data:list);

        }

        /// <summary>
        /// 房型更新
        /// </summary>
        /// <param name="RoomTypeUpdateDTO">实体</param>
        /// <returns>返回实体</returns>
        //[HttpPut("RoomTypeUpdate")]
        //public async Task<ApiResult> RoomTypeUpdate(RoomTypeUpdateDTO RoomTypeUpdateDTO)
        //{
        //    try
        //    {
        //        var roomType = await _roomTypeRepository.FirstOrDefaultAsync(x => x.Id == RoomTypeUpdateDTO.Id);
        //        if (roomType == null)
        //        {
        //            return ApiResult.Fail(msg: "RoomType not found");
        //        }

        //        ObjectMapper.Map(RoomTypeUpdateDTO, roomType);
        //        await _roomTypeRepository.UpdateAsync(roomType);
        //        return ApiResult.Ok(data: roomType);
        //    }
        //    catch (Exception ex)
        //    {
        //        return ApiResult.Fail(msg: ex.Message);
        //    }
        //}




    }
}
